<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数组嵌套深度计算</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f7fc;
            color: #333;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            flex-direction: column;
        }

        h1 {
            color: #4CAF50;
            font-size: 2rem;
            margin-bottom: 20px;
        }

        .container {
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            padding: 20px;
            width: 80%;
            max-width: 600px;
            text-align: center;
        }

        textarea {
            width: 100%;
            height: 100px;
            margin-bottom: 15px;
            padding: 10px;
            border-radius: 5px;
            border: 1px solid #ccc;
            font-size: 1rem;
            font-family: monospace;
        }

        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 1rem;
        }

        button:hover {
            background-color: #45a049;
        }

        .result {
            margin-top: 20px;
            font-size: 1.2rem;
            font-weight: bold;
        }
    </style>
</head>

<body>
    <h1>数组嵌套深度计算器</h1>
    <div class="container">
        <textarea id="array-input" placeholder='例如：[[1, 2], [3, [4, 5]], 6]'></textarea>
        <button onclick="calculateDepth()">计算嵌套深度</button>
        <div class="result" id="depth-result"></div>
    </div>

    <script>
        // 计算数组嵌套深度的函数
        const getArrayDepth = (arr) => {
            if (!Array.isArray(arr)) {
                return 0;
            }
            if (arr.length === 0) {
                return 1;
            }
            let maxDepth = 1;
            for (let i = 0; i < arr.length; i++) {
                const depth = getArrayDepth(arr[i]);
                maxDepth = Math.max(maxDepth, depth + 1);
            }
            return maxDepth;
        }

        // 获取用户输入并计算深度的函数
        function calculateDepth() {
            const input = document.getElementById('array-input').value;

            // 尝试将输入的字符串转换为数组
            let arr;
            try {
                arr = eval(input);
                if (!Array.isArray(arr)) {
                    throw new Error('输入的内容不是一个数组');
                }
            } catch (e) {
                document.getElementById('depth-result').innerText = '请输入有效的数组格式！';
                return;
            }

            // 计算深度并显示结果
            const depth = getArrayDepth(arr);
            document.getElementById('depth-result').innerText = `数组的嵌套深度为：${depth}`;
        }
    </script>
</body>

</html>